//
//  AIGirlAccountViewController.swift
//  AIGirl
//
//  Created by Rae on 2023/12/28.
//

import UIKit

class AIGirlAccountViewController: AIGirlBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("AIGirl_Profile_account_back").end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(AIGirlAccountItem.self)
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = 80
        return tableView
    }()
    var itemInfos: [AIGirlProfileModel] = [.signOut, .delete]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.subviews{
            backButton
            tableView
        }
        
        backButton.leading(15).top(CGFloat.statusHeight + 5.0).size(30)
        tableView.leading(0).trailing(0).bottom(0)
        tableView.Top == backButton.Bottom + 9
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
}

extension AIGirlAccountViewController : UITableViewDelegate, UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemInfos.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.cell(AIGirlAccountItem.self, indexPath: indexPath)
        cell.model = itemInfos[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = itemInfos[indexPath.row]
        if model == .delete {
            let delView = AIGirlDeleteView()
            delView.deleteClosure = {
                self.request.start(.AIGirlDelAccount) { result in
                    if result.isSuccessful {
                        "Success!".toast_success
                        UserDefaultsManager.clearUserDefaults()
                        UIApplication.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlWelcomeViewController())
                    }
                }
            }
            animation.animation(show: delView)
        } else if model == .signOut {
            UserDefaultsManager.clearUserDefaults()
            UIApplication.window?.rootViewController = AIGirlNavViewController(rootViewController: AIGirlWelcomeViewController())
        }
    }
}

class AIGirlAccountItem: UITableViewCell {

    private let backView = c_make_ex(UIView.self)
        .bgColor("#FBFBFB".toColor.alpha(0.06)).corner(21).end()
    private let titleLabel = c_make_ex(UILabel.self)
        .font(17.font_medium).color(.white).end()
    private let rightImageView = c_make_ex(UIImageView.self)
        .image("AIGirl_Profile_right").end()
    
    var model: AIGirlProfileModel = .none {
        didSet{
            titleLabel.text = model.title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                titleLabel
                rightImageView
            }
        }
        
        backView.leading(15).trailing(15).top(10).height(70)
        titleLabel.leading(15).centerVertically()
        rightImageView.trailing(15).width(12).height(22).centerVertically()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
